package com.czwj.util;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import net.sf.json.JSONObject;

public class GsonUtil<T> {

	private static Gson gson = new Gson();

	public static <T> String objectToJsonStr(T object) {
		String jsonStr;
		jsonStr = gson.toJson(object);
		return jsonStr;
	}

	public static <T> String objectsToJsonStr(List<T> objects) {
		String jsonStr;
		jsonStr = gson.toJson(objects);
		return jsonStr;

	}

	public static <T> T jsonStrToObject(String jsonStr, Class<T> cla) {
		T object = gson.fromJson(jsonStr, cla);
		return object;
	}

	public static <T> List<T> jsonStrToObjects(String jsonStr, Class<T> cla) {
		List<T> listT = null;
		try {
			T t = cla.newInstance();
			Class<T> classT = (Class<T>) Class.forName(t.getClass().getName());
			List<Object> listObj = new ArrayList<Object>();
			listObj = new GsonBuilder().create().fromJson(jsonStr, new TypeToken<List<Object>>() {
			}.getType());
			if (listObj == null || listObj.isEmpty())
				return null;
			listT = new ArrayList<T>();
			for (Object obj : listObj) {
				T perT = new GsonBuilder().create().fromJson(obj.toString(), classT);
				listT.add(perT);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listT;
	}
	
		
		
	public static void toJson(HttpServletResponse response, Object data) 
	        throws IOException {
	        Gson gson = new Gson();
	        String result = gson.toJson(data);
	        response.setContentType("text/json; charset=utf-8");
	        response.setHeader("Cache-Control", "no-cache");
	        OutputStream out = response.getOutputStream();
	        out.write(result.getBytes("utf-8"));
	        out.flush();
	        out.close();
	    }

	/**
	     * 处理JSON对象数据据包，以<键，值>对的形式存入java.util.Map
	     * @param String
	     * @return java.util.Map
	     */
		public static Map<String, Object> getKeytoValueFromJsonObject(String jsonStr){
			JSONObject jsonObject = JSONObject.fromObject(jsonStr);
			Map<String, Object> map = jsonObject;
			return map;
		}

}